package me.zhengjie.cron;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.sqlServer.PPInfo;
import me.zhengjie.pojo.sqlServer.PPInventory;
import me.zhengjie.ppService.PPInventoryService;
import me.zhengjie.repository.PPInfoRepository;
import me.zhengjie.repository.PPInventoryRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: xinglin
 * @CreateTime: 2024-08-03
 * @Description: 定时查询pp是否预期和超期还有过期
 * @Version: 1.0
 */


@Component
@RequiredArgsConstructor
public class PPInventoryCron {

    private final PPInventoryRepository ppInventoryRepository;
    private final PPInventoryService ppInventoryService;
    private final PPInfoRepository ppInfoRepository;

    private final static Logger ppLog = LoggerFactory.getLogger("pp");


    @Scheduled( fixedDelay = 3000)
    public void ppInventoryCron() {
        try {
            ppInventoryService.updateStayStatus();
            ppInventoryRepository.findAll().stream().forEach(
                    pp -> {
                        if (pp.getDateStatus().equals("warning")) {

//                            ppLog.info("pp批号为" + pp.getPpBatch() + "预警" + LocalDateTime.now());
                        } else if (pp.getDateStatus().equals("outTime")) {

//                            ppLog.info("pp批号为" + pp.getPpBatch() + "超期" + LocalDateTime.now());
                        } else if (pp.getDateStatus().equals("exp")) {

//                            ppLog.info("pp批号为" + pp.getPpBatch() + "过期" + LocalDateTime.now());

                        }
                    }
            );
        } catch (Exception e) {
            ppLog.error("ppInventoryCron error:{}", e.toString());
        }

    }

    @Scheduled(cron = "0 0 0 1 * *")
    public void deletePpInventory() {
        //删除出库在外时间为90的库存
        try {
            //查找所有在外的库存
            List<PPInventory> ppInventoryList = ppInventoryRepository.findAll()
                    .stream().filter(i->i.getStayStatus()==1).collect(Collectors.toList());

            List<PPInventory>  outTimeList =  new ArrayList<>();
            for (PPInventory ppInventory : ppInventoryList) {
                //如果出库在外时间大于90天，则删除
                LocalDateTime outTime = ppInventory.getOutBoundTime();
                if (outTime != null && outTime.plusDays(90).isBefore(LocalDateTime.now())) {
                    outTimeList.add(ppInventory);
                    ppInventoryRepository.deleteById(ppInventory.getId());
                    //找到对应PPinfo也删了
//                    PPInfo ppInfo =ppInfoRepository.getPPInfoByPpBatch(ppInventory.getPpBatch());
//                    ppInfoRepository.delete(ppInfo);
                }
            }
            List<Integer> ids = outTimeList.stream().map(PPInventory::getId).collect(Collectors.toList());

        }
        catch (Exception e) {
            ppLog.error("deletePpInventoryCron error:{}", e.toString());
        }

    }


}
